<?php
function register($name, $pass, $msg)
{
    session_start();
    $email = $_SESSION['email'];
    if (!empty($email)) {
        return True;
    }
    $db = mysql_connect("localhost", "hdfs", "hdfs");
    if (!$db) {
        $msg = "Colud not connect: " . mysql_error();
        return False;
    }
    if (!mysql_select_db("hadoop", $db)) {
        $msg = "Colude not select db hadoop: " . mysql_error();
        mysql_close($db);
        return False;
    }
    $query = sprintf("select count(*) from users where email = '%s';",
            mysql_real_escape_string($name));
    $result = mysql_query($query, $db);
    if (!$result) {
        $msg = "Register user: $name error: " . mysql_error();
        mysql_close($db);
        return False;
    }
    $count = mysql_fetch_array($result);

    if ($count[0] > 0) {
        $msg = "Email already exist";
        mysql_close($db);
        return False;
    }
    
    $query = sprintf("insert into users (email, passwd) values ('%s', '%s');",
            mysql_real_escape_string($name),
            mysql_real_escape_string($pass));
    $result = mysql_query($query, $db);
    if (!$result) {
        $msg = "Register user: $name error: " . mysql_error();
        mysql_close($db);
        return False;
    }
    mysql_close($db);
    return True;
}

function regform($msg)
{
?>
    <html>
    <body>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <h1>Hadoop Job Commit</h1>
    <title>User Register</title>
    </head>

    <form action="index.php?register=true" method="post">
    <table width="400">
    <tr>
        <td scope="row">Mail:</td>
        <td > 
        <div align="left">
            <input type="text" name="name" />@tinaya.cn
        </div>
        <td>
    </tr>
    <tr>
        <td scope="row">Password:</td>
        <td><input type="password" name="pass" /></td>
    </tr>
    <tr>
        <td scope="row">Password Again:</td>
        <td><input type="password" name="pass_again" /></td>
    </tr>
    <tr>
        <td colspan="2" scope="row" align="center">
            <input name="login" type="submit" value="Register" />
        </td>
    </tr>
    </table>
    </form>
    <a href=index.php?login>Login</a>
<?
    if ($msg) {
?>
    <p><b> <?echo $msg;?> </b></p>
<?
    }
?>
    </body>
    </html>
<?
}

function commitform($name)
{
?>
    <html>
    <body>
    <head>
    <h1>Hadoop Job Commit</h1>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <title>Commit Map/Reduce Job</title>
    <script>
    function complete2text(str) {
        var array = str.split("|");
        document.getElementById('text_id').value = array[1];
    }
    </script>
    </head>

    <p><b>Email: <?echo $name;?><b></p>
    <form enctype="multipart/form-data" action="commit.php?run" method="post">
    <table width="100">
        <tr>
        <td><label>Jobs:</label></td>
        <td>
    <select name="jobs" size=1 id="jobs_id" style="width:300px;" onchange="complete2text(this.value);">Jobs
<?
    $db = mysql_connect("localhost", "hdfs", "hdfs");
    if (!$db) {
        die("Colud not connect: " . mysql_error());
        return;
    }
    if (!mysql_select_db("hadoop", $db)) {
        die("Colude not select db hadoop: " . mysql_error());
        mysql_close($db);
        return;
    }
    $query = "select jar,args from jobs;";
    $result = mysql_query($query);
    if (!$result) {
        die("select jobs error: " . mysql_error());
        mysql_close($db);
        return;
    }
    echo "<option />";
    while ($rows = mysql_fetch_array($result, MYSQL_NUM)) {
        echo "<option value=\"".$rows[0]."|".$rows[1]."\">".basename($rows[0])."</option>";
    }
    mysql_close($db);
?>
    </select>
    </td>
    </tr>

    <tr>
    <td><label>Arguments:</label></td>
    <td><input type="text" name="exist_args" size=40 id="text_id"/></td>
    </tr>

    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="Run the Job" name="run" />
        </td>
    </tr>
    </table>
    </form>

    <form enctype="multipart/form-data" action="commit.php?commit" method="post">
    <table width="100">
    <tr>
        <td scope="row">Jar File:</td>
        <td > 
        <div align="left">
            <input type="file" name="file" size=40/>
        </div>
        </td>
        
    </tr>
    <tr>
        <td scope="row">Arguments:</td>
        <td><input type="text" name="args" size=40/></td>
    </tr>
    <tr>
        <td colspan="2" scope="row" align="center">
            <input name="login" type="submit" value="Commit" />
        </td>
    </tr>
    </table>
    </form>

<?
    if ($msg) {
?>
    <p><b> <?echo $msg;?> </b></p>
<?
    }
?>
    <a href=http://124.225.213.82:50070>BROWSE HDFS</a>
    <p></p>
    <a href=http://124.225.213.82:60010>BROWSE HBAS</a>
    </body>
    </html>
<?

}

function defaults($msg)
{
?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <h1>Hadoop Job Commit</h1>
    <title>Login Form</title>
    </head>
    <body>

    <form action="index.php?login=true" method="post">
    <table width="400">
    <tr>
        <th width="27%" scope="row">Mail:</th>
        <td width="73%"> 
        <div align="left">
            <input type="text" name="name" size=20/>@tianya.cn
        </div>
        <td>
    </tr>
    <tr>
        <th scope="row">Password:</th>
        <td><input type="password" name="pass" size=20/></td>
    </tr>
    <tr>
        <td colspan="2" scope="row">
        <div align="center">
            <input name="login" type="submit" value="Login" />
        </div>
        </td>
    </tr>
    <tr>
        <td scop="row">
        <div align="left">
            <a href=index.php?register>Not Register?</a>
        </div><td>
    </tr>
    </table>
    </form>
<?
    if ($msg) {
?>
    <p> <b><?echo $msg;?></b> </p>
<?
    }
?>

    </body>
    </html>
<?
}

function check($name, $pass, $msg)
{
    $db = mysql_connect("localhost", "hdfs", "hdfs");
    if (!$db) {
        $msg = "Colud not connect: " . mysql_error();
        return False;
    }
    if (!mysql_select_db("hadoop", $db)) {
        $msg = "Colude not select db hadoop: " . mysql_error();
        mysql_close($db);
        return False;
    }
    $query = sprintf("select count(*) from users where email = '%s' and passwd = '%s';",
            mysql_real_escape_string($name),
            mysql_real_escape_string($pass));
    $result = mysql_query($query, $db);
    if (!result) {
        $msg = "Login error: " . mysql_error();
        mysql_close($db);
        return False;
    }
    $count = mysql_fetch_array($result);

    if ($count[0] != 1) {
        $msg = "Email or Password not match";
        mysql_close($db);
        return False;
    }
    mysql_close($db);
    return True;
}

session_start();
$email = $_SESSION['email'];
if (!empty($email)) {
    commitform($email);
    return;
}

if ($_GET['login'] == 'true') {
    $name = trim($_POST['name']);
    $pass = trim($_POST['pass']);
    if (empty($name) || empty($pass)) {
        defaults("Mail and Password must be specified");
    } else {
        $name = $name . "@tianya.cn";
        if (!check($name, $pass, &$msg)) {
            defaults($msg);
        } else {
            $_SESSION['email'] = $name;
            commitform($name);
        }
    }
} else if ($_GET['register'] == 'true'){
    $name = trim($_POST['name']);
    $pass = trim($_POST['pass']);
    $pass_again = trim($_POST['pass_again']);
    $pattern = "^[a-zA-Z0-9]+@tianya.cn$";
    if (empty($name) || empty($pass) || empty($pass)) {
        regform("Email and Password cant be empty");
    } else if (!ereg($pattern, $name."@tianya.cn")) {
        regform("Must tianya email");
    } else if ($pass != $pass_again) {
        regform("tow Password not match");
    } else {
        $name = $name . "@tianya.cn";
        if (!register($name, $pass, &$msg)) {
            regform($msg);
        } else {
            $_SESSION['email'] = $name;
            commitform($name);
        }
    }
} else if (isset($_GET['register'])) {
    regform("");
} else {
    defaults("");
}
?>
